function HandlePageCustomers(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._createElements();
}

HandlePageCustomers.prototype._createElements = function() {
  
  this._popupBox = new PopupBox();
  this._query = DlmanageUtils.query();
  this._search = DlmanageUtils.search();

  this._gui.customers.appendChild(DOMUtils.getLoadingImage());
  
  this._loadData();
};

HandlePageCustomers.prototype._loadData = function() {
  this._customers = null;
  this._parameters = null;
  this._retrieveCustomers(1);
};

HandlePageCustomers.prototype._verifyData = function() {
  if (this._customers) {
    this._updateElements();
  }
};

HandlePageCustomers.prototype._retrieveCustomers = function(page, condition, date, from, to, pos) {
  var _self = this;
  var cond = (this._operator.type == '{{$smarty.const.USER_TYPE_ADMIN|escape:javascript}}') ? 1 : 0;
  var args = ((condition) ?  '&c=' + encodeURIComponent(condition) : '') + '&p=' + page + '&s={{$smarty.const.SIZE|escape:'javascript'}}' + ((date) ?  '&date=' + date : '') + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '') + '&cond=' + cond;
  /*new RequestUtils()._mysql('customers', args, function(result, params) { _self._customers = result.data;
                                                                          _self._parameters = result;
                                                                          _self._verifyData.call(_self);
                                                                        }, {pos: (pos) ? pos : null });
*/

  new RequestUtils()._mysql('v1customer', args, function(result, params) { _self._customers = result.data;
                                                                          _self._parameters = result;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php',pos: (pos) ? pos : null }); 
};

HandlePageCustomers.prototype._updateElements = function() {
  var unique, _self = this;
  DOMUtils.removeChildElements(this._gui.pages);
  DOMUtils.removeChildElements(this._gui.customers);
  
  
  Pagination.makePagedResults(this._gui.pages, this._parameters.page, this._parameters.total, this._parameters.size, function(page, condition) { _self._retrieveCustomers.call(_self, page, _self._parameters.condition); }, this, document);

  var table = document.createElement('table');
  table.cellPadding = 0;
  table.cellSpacing = 0;
  this._gui.customers.appendChild(table);
  if (this._customers.length == 0) {
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.width = '900px';
    td.style.height = '25px';
    td.style.borderRight = 'solid 1px #8F8F8A';
    td.style.backgroundImage = 'url("images/common/headline.png")';
    td.style.backgroundRepeat = 'repeat-x';
    td.style.fontSize = '11pt';
    td.style.color = '#633e36';
    td.style.fontWeight = 'bold';
    td.style.paddingLeft = '15px';
    td.appendChild(document.createTextNode('Customer'));
    
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.width = '900px';
    td.style.height = '75px';
    td.style.backgroundColor = '#fff';
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }

  for(var i = 0, il = this._customers.length; i < il; i++) {
    var customer = this._customers[i];
    new IContainerCustomer(table, customer, {operator: this._operator});
  }
  
  this._customerSearch(this._gui, function(condition, date, from, to, pos) { _self._retrieveCustomers.call(_self, 1, condition, date, from, to, pos); });
};

HandlePageCustomers.prototype._customerSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;
  this._search.id = gui.search.customerNo.value;
  gui.search.customerNo.value = this._search.id;
  gui.search.customerNo.onkeydown = function() { _self._search.id = this.value; };
  gui.search.customerNo.onchange = function() { _self._search.id = this.value; };


  gui.search.customerNo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                   if (code == 13) {
                                                     _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                                   }
                                                 };
  
  gui.search.customerNo.onkeyup = function() { if (gui.search.customerNo.value.length == 0) {
                                                 _self._popupBox._close();
                                               } else {
                                                 var div = document.createElement('div');
                                                 var pos1 = DOMUtils.findPos(gui.search.customerNo);
                                                 var pos = [pos1[0] + 1, pos1[1] + 82];
                                                 var query = "SELECT c.e_oid as id FROM ns_v1customer as c WHERE 1 = 1 AND " + '((c.e_oid LIKE \'%' + gui.search.customerNo.value + '%\'))' + " ";
                                                 MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                                 new DialogSearchResult(div, gui.search.customerNo, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();}, php: 'customphp/mysqlread.php'});
                                               }
                                             };
  
  this._search.name = gui.search.name.value;
  gui.search.name.value = this._search.name;
  gui.search.name.onkeydown = function() { _self._search.name = this.value; };
  gui.search.name.onchange = function() { _self._search.name = this.value; };
  gui.search.name.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                             if (code == 13) {
                                               _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                             }
                                           };
  gui.search.name.onkeyup = function() { if (gui.search.name.value.length == 0) {
                                           _self._popupBox._close();
                                         } else {
                                           var div = document.createElement('div');
                                           var pos1 = DOMUtils.findPos(gui.search.name);
                                           var pos = [pos1[0] + 1, pos1[1] + 82];
                                           var query = "SELECT c.bridename,c.groomname FROM ns_v1customer as c  WHERE 1 = 1 AND " + '((c.bridename LIKE \'%' + gui.search.name.value + '%\'))' + " OR " + '((c.groomname LIKE \'%' + gui.search.name.value + '%\'))' + " ";
                                           MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                           new DialogSearchResult(div, gui.search.name, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();},php:'customphp/mysqlread.php'});
                                         }
                                       };
  
  this._search.phone = gui.search.phone.value;
  gui.search.phone.value = this._search.phone;
  gui.search.phone.onkeydown = function() { _self._search.phone = this.value; };
  gui.search.phone.onchange = function() { _self._search.phone = this.value; };
  gui.search.phone.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                              if (code == 13) {
                                                _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                              }
                                            };
  gui.search.phone.onkeyup = function() { if (gui.search.phone.value.length == 0) {
                                            _self._popupBox._close();
                                          } else {
                                            var div = document.createElement('div');
                                            var pos1 = DOMUtils.findPos(gui.search.phone);
                                            var pos = [pos1[0] + 1, pos1[1] + 82];
                                            var query = "SELECT c.bridephone,c.bridemobile,c.groomphone,c.groommobile FROM ns_v1customer as c WHERE 1 = 1 AND " + '( (c.bridephone LIKE \'%' + gui.search.phone.value + '%\') OR (c.bridemobile LIKE \'%' + gui.search.phone.value + '%\') OR (c.groomphone LIKE \'%' + gui.search.phone.value + '%\') OR (c.groommobile LIKE \'%' + gui.search.phone.value + '%\') )' + " ";
                                            MiscUtils.dialog(_self._popupBox, pos, div, null, null, {pop: true});
                                            new DialogSearchResult(div, gui.search.phone, { query: query, popupBox: _self._popupBox, callbackFunc: function() { _self._updateElements();},php:'customphp/mysqlread.php'});
                                          }
                                        };
  gui.search.wd.value = (this._search.weddingDay) ? SimpleDate.format(this._search.weddingDay) : '';
  gui.search.wd.onclick = function() { var context = this;
                                       _self._changeDate.call(_self, this, _self._search.weddingDay, function(sd) { _self._search.weddingDay = sd; context.focus();}, false);
                                       return false;
                                     };
  gui.search.wd.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                           if (code == 13) {
                                             _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                           }
                                         };
  gui.search.createdFrom.value = (this._search.dateFrom) ? SimpleDate.format(this._search.dateFrom) : '';
  gui.search.createdFrom.onclick = function() { var context = this;
                                                _self._changeDate.call(_self, this, _self._search.dateFrom, function(sd) { _self._search.dateFrom = sd; context.focus(); }, false);
                                                return false;
                                              };
  gui.search.createdFrom.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                    if (code == 13) {
                                                      _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                                    }
                                                  };
  gui.search.createdTo.value = (this._search.dateTo) ? SimpleDate.format(this._search.dateTo) : '';
  gui.search.createdTo.onclick = function() { var context = this;
                                              _self._changeDate.call(_self, this, _self._search.dateTo, function(sd) { _self._search.dateTo = sd; context.focus(); }, false);
                                              return false;
                                            };
  gui.search.createdTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                  if (code == 13) {
                                                    _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this));
                                                  }
                                                };
  gui.search.wedding.defaultChecked = this._search.orderWedding;
  gui.search.wedding.onclick = function() { _self._search.orderWedding = this.checked; 
                                            if (!_self._search.orderWedding) {
                                              for (var i = 0, il = _self._query.length; i < il; i++) {
                                                if (_self._query[i].id == 1) {
                                                  var m = i;
                                                } 
                                              }
                                              _self._query.splice(m, 1);
                                            } else {
                                              for (var j = 0, jl = DlmanageUtils.query().length; j < jl; j ++) {
                                                var string = DlmanageUtils.query()[j];
                                                if (string.id == 1) {
                                                  _self._query[_self._query.length] = string;
                                                }
                                              }
                                            }
                                          };
  
  gui.search.preWedding.defaultChecked = this._search.orderPrewedding;
  gui.search.preWedding.onclick = function() { _self._search.orderPrewedding = this.checked; 
                                               if (!_self._search.orderPrewedding) {
                                                 for (var i = 0, il = _self._query.length; i < il; i++) {
                                                   if (_self._query[i].id == 2) {
                                                     var m = i;
                                                   } 
                                                 }
                                                 _self._query.splice(m, 1);
                                               } else {
                                                 for (var j = 0, jl = DlmanageUtils.query().length; j < jl; j ++) {
                                                   var string = DlmanageUtils.query()[j];
                                                   if (string.id == 2) {
                                                     _self._query[_self._query.length] = string;
                                                   }
                                                 }
                                               }
                                             };
  
  gui.search.portrait.defaultChecked = this._search.orderPortrait;
  gui.search.portrait.onclick = function() { _self._search.orderPortrait = this.checked; 
                                             if (!_self._search.orderPortrait) {
                                               for (var i = 0, il = _self._query.length; i < il; i++) {
                                                 if (_self._query[i].id == 3) {
                                                   var m = i;
                                                 } 
                                               }
                                               _self._query.splice(m, 1);
                                             } else {
                                               for (var j = 0, jl = DlmanageUtils.query().length; j < jl; j ++) {
                                                 var string = DlmanageUtils.query()[j];
                                                 if (string.id == 3) {
                                                   _self._query[_self._query.length] = string;
                                                 }
                                               }
                                             }
                                           };
  
  gui.search.engagement.defaultChecked = this._search.orderEngagement;
  gui.search.engagement.onclick = function() { _self._search.orderEngagement = this.checked; 
                                               if (!_self._search.orderEngagement) {
                                                 for (var i = 0, il = _self._query.length; i < il; i++) {
                                                   if (_self._query[i].id == 4) {
                                                     var m =i;
                                                   } 
                                                 }
                                                 _self._query.splice(m, 1);
                                               } else {
                                                 for (var j = 0, jl = DlmanageUtils.query().length; j < jl; j ++) {
                                                   var string = DlmanageUtils.query()[j];
                                                   if (string.id == 4) {
                                                     _self._query[_self._query.length] = string;
                                                   }
                                                 }
                                               }
                                             };
  
  gui.search.other.defaultChecked = this._search.orderOther;
  gui.search.other.onclick = function() { _self._search.orderOther = this.checked; 
                                          if (!_self._search.orderOther) {
                                            for (var i = 0, il = _self._query.length; i < il; i++) {
                                              if (_self._query[i].id == 5) {
                                                var m =i;
                                              } 
                                            }
                                            _self._query.splice(m, 1);
                                          } else {
                                            for (var j = 0, jl = DlmanageUtils.query().length; j < jl; j ++) {
                                              var string = DlmanageUtils.query()[j];
                                              if (string.id == 5) {
                                                _self._query[_self._query.length] = string;
                                              }
                                            }
                                          }
                                        };

  gui.search.search.onclick = function() { _self._callbackFunc.call(_self, _self._toString(_self._search), (_self._search.weddingDay) ? JSON.stringify(_self._search.weddingDay) : null, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null, DOMUtils.findPos(this)); };
};

HandlePageCustomers.prototype._toString = function(search) {
  var str = 'WHERE ';
  var str0 = '';
  
  str += (search.name == '') ? '1 = 1' : '((c.bridename LIKE \'%' + search.name + '%\') OR (c.groomname LIKE \'%' + search.name + '%\'))';
  str += (search.phone == '') ? '' : ' AND ( ( (c.bridephone LIKE \'%' + search.phone + '%\') OR (c.groomphone LIKE \'%' + search.phone + '%\') ) OR ((c.bridemobile LIKE \'%' + search.phone + '%\') OR (c.groommobile LIKE \'%' + search.phone + '%\') ) )';
  
  str += (search.id == '') ? '' : ((isNaN(search.id)) ? ' AND 1 = 0' : ' AND (c.e_oid=\'' + search.id + '\')');
  
  if (search.name != '' || search.phone != '') {
    str0 += '';
  }
  
  if (search.weddingDay || this._query.length != 5 || this._gui.search.finished.checked == true) {
    str0 += ' left join ns_v1myorder ' + ' AS o ON o.customerId = c.e_oid';
  }
  if(this._gui.search.finished.checked == true){
    str += ' AND c.e_oid not in (SELECT   DISTINCT customerId FROM ns_v1myorder  WHERE customerId = c.e_oid AND finished = 0 ) AND o.disposalstatus = 0'; 
  }

  if (this._query.length != 5 && this._query.length != 0) {
    for (var i = 0, il = this._query.length; i < il; i++) {
      var qy = this._query[i];
      
      if (i == 0) {
        str += ' AND (';
      }
      str += qy.string;
      
      if (i == this._query.length - 1) {
        str += ')';
      } else {
        str += ' OR ';
      }
    }
  } else if (this._query.length == 0) {
    str += ' AND o.type != \'' + Constant.ORDER_TYPE_ENGAGEMENT +
           '\' AND o.type != \'' + Constant.ORDER_TYPE_OTHER +
           '\' AND o.type != \'' + Constant.ORDER_TYPE_PREWEDDING +
           '\' AND o.type != \'' + Constant.ORDER_TYPE_PORTRAIT +
           '\' AND o.type != \'' + Constant.ORDER_TYPE_WEDDING + '\'';
  }
  return (str0 + ' ' + str);
};

HandlePageCustomers.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageCustomers.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageCustomers.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};